package com.test.down.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 隐藏输入法
     *
     * @param activity
     */
    public static void hideSoftKeyboard(Activity activity) {
        View view = activity.getCurrentFocus();
        if (null != view && null != view.getWindowToken()) {
            ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow
                    (view.getWindowToken(), 0);
        }
    }

    /**
     * 隐藏输入法(dialog中)
     *
     * @param context
     * @param window
     */
    public static void hideSoftKeyboard(Context context, Window window) {
        View view = window.getCurrentFocus();
        if (null != view && null != view.getWindowToken()) {
            ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow
                    (view.getWindowToken(), 0);
        }
    }

    /**
     * 显示软键盘
     *
     * @param activity
     * @param editText
     */
    @TargetApi(Build.VERSION_CODES.CUPCAKE)
    public static void showSoftKeyBoard(Activity activity, EditText editText) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        editText.requestFocus();
        imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
    }

    /**
     * 底部状态栏高度
     *
     * @param activity
     * @return
     */
    public static int getNavigationBarHeight(Activity activity) {
        try {
            Resources resources = activity.getResources();
            int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
            int height = resources.getDimensionPixelSize(resourceId);
            return height;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 验证是否全是数字
     *
     * @param num
     * @return
     */
    public static boolean validNum(String num) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher match = pattern.matcher(num + "");
        return match.matches();
    }

}
